package org.example.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class StreamTest1 {
    public static void main(String[] args) {
        List<Optional<Customer>> customers = Arrays.asList(
                Optional.of(new Customer("日供一并",18)),
                Optional.of(new Customer("卑微的小开发",22)),
                Optional.empty(),
                Optional.of(new Customer("00T",22)),
                Optional.empty(),
                Optional.of(new Customer("温柔一刀",23)),
                Optional.empty()
        );
        long numberOf65PlusCustomers = customers.stream()
                .flatMap(c -> c.map(Stream::of).orElseGet(Stream::empty))
                .filter(c -> c.getAge() > 18)
                .count();
        System.err.println(numberOf65PlusCustomers);
    }
    static class Customer{
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Customer(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
}
